package org.charles.zhifou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.charles.zhifou.common.response.ResponseCodeEnum;
import org.charles.zhifou.enums.UserRoleEnum;
import org.charles.zhifou.common.exception.BusinessException;
import org.charles.zhifou.common.exception.ThrowUtils;
import org.charles.zhifou.mapper.ScoringResultMapper;
import org.charles.zhifou.model.entity.App;
import org.charles.zhifou.model.entity.ScoringResult;
import org.charles.zhifou.model.entity.User;
import org.charles.zhifou.service.AppService;
import org.charles.zhifou.service.ScoringResultService;
import org.charles.zhifou.service.UserService;
import org.springframework.stereotype.Service;

/**
* @author charles
* @description 针对表【scoring_result(评分结果)】的数据库操作Service实现
* @createDate 2024-08-06 09:11:36
*/
@Service
public class ScoringResultServiceImpl extends ServiceImpl<ScoringResultMapper, ScoringResult>
    implements ScoringResultService {

    @Resource
    private AppService appService;

    @Resource
    private UserService userService;

    @Override
    public Long saveScoringResult(ScoringResult scoringResult) {
        // 判断应用是否存在
        App app = appService.getById(scoringResult.getAppId());
        ThrowUtils.throwIf(app == null, ResponseCodeEnum.PARAMS_ERROR, "应用不存在");

        // 保存进数据库
        boolean isSuccessful = this.save(scoringResult);
        ThrowUtils.throwIf(!isSuccessful, ResponseCodeEnum.OPERATION_ERROR, "写入数据库失败");

        return scoringResult.getId();
    }

    @Override
    public Boolean removeScoringResultById(Long id) {
        // 判断评分结果是否存在
        ScoringResult scoringResultToRemove = this.getById(id);
        ThrowUtils.throwIf(scoringResultToRemove == null, ResponseCodeEnum.PARAMS_ERROR, "评分结果不存在");

        // 仅本人或管理员可删除
        User loginUser = userService.getLoginUser();
        if (!scoringResultToRemove.getUserId().equals(loginUser.getId()) && !UserRoleEnum.ADMIN.equals(loginUser.getUserRole())) {
            throw new BusinessException(ResponseCodeEnum.NO_AUTH_ERROR);
        }

        // 删除评分结果
        return this.removeById(id);
    }

    @Override
    public Boolean updateScoringResultById(ScoringResult scoringResult) {
        // 判断评分结果是否存在
        ScoringResult scoringResultToUpdate = this.getById(scoringResult.getId());
        ThrowUtils.throwIf(scoringResultToUpdate == null, ResponseCodeEnum.PARAMS_ERROR, "评分结果不存在");

        return this.updateById(scoringResult);
    }

    @Override
    public Boolean editScoringResultById(ScoringResult scoringResult) {
        // 判断评分结果是否存在
        ScoringResult scoringResultToEdit = this.getById(scoringResult.getId());
        ThrowUtils.throwIf(scoringResultToEdit == null, ResponseCodeEnum.PARAMS_ERROR, "评分结果不存在");

        // 仅本人或管理员可编辑
        User loginUser = userService.getLoginUser();
        if (!scoringResultToEdit.getUserId().equals(loginUser.getId()) && !UserRoleEnum.ADMIN.equals(loginUser.getUserRole())) {
            throw new BusinessException(ResponseCodeEnum.NO_AUTH_ERROR);
        }

        return this.updateById(scoringResult);
    }
}




